<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>flf-文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <script src="./libs/layui/lay/modules/layer.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <div class="row" style="margin-top: 10px;">
                <div
                    class="col-xs-offset-10 col-sm-offset-10 col-md-offset-10 col-lg-offset-10 col-xs-2 col-sm-2 col-md-2 col-lg-2">
                    <a href="javascript:void(0)" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                        data-target="#myModal">新增分类</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead id="thead">
                    <tr>
                        <th>名称</th>
                        <th>描述</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody id="tbody">
                    <tr>
                        <td>爱生活</td>
                        <td>热爱生活</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="javascript:void(0)" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <!-- 修改分类布局区域 -->
    <div class="modal fade" id="change_myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">修改分类</h4>
                </div>
                <div class="modal-body">
                    <div class="add_category">
                        <div class="form-group">
                            <label for="name">分类名</label>
                            <input type="text" class="form-control" id="change_name" placeholder="分类名">
                        </div>
                        <div class="form-group">
                            <label for="slug">分类别名</label>
                            <input type="text" class="form-control" id="change_slug" placeholder="分类别名">
                        </div>
                        <div class="modal-footer">
                            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                            <button type="button" id="edit" class="btn btn-primary btn_opt1">保存修改</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <!-- 新增分类布局区域 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新增分类</h4>
                </div>
                <div class="modal-body">
                    <div class="add_category">
                        <div class="form-group">
                            <label for="name">分类名</label>
                            <input type="text" class="form-control" id="name" placeholder="分类名">
                        </div>
                        <div class="form-group">
                            <label for="slug">分类别名</label>
                            <input type="text" class="form-control" id="slug" placeholder="分类别名">
                        </div>
                        <div class="modal-footer">
                            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                            <button type="button" class="btn btn-primary btn_opt">保存</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>


    <script src="./js/http.js"></script>
    <!-- 引入 template js -->
    <script src="../js/template-web.js"></script>

    <script id="art_list" type="text/html">

    <!-- 请求获取的数据遍历生成列表 -->
    {{each data}}
    <tr>
        <td>{{$value.name}}</td>
        <td>{{$value.slug}}</td>
        <td class="text-center">
            <a href="javascript:void(0)" onclick="edit_open({{$value.id}})" data-toggle="modal"
                class=" btn btn-info btn-xs">编辑</a>
            <a href="javascript:void(0)" onclick="del_class({{$value.id}})" class="btn btn-danger btn-xs">删除</a>
        </td>
    </tr>
    {{/each}}
    </script>

    <script>
        $(function () {
            /* 1.0 调用获取文章分类数据函数 */
            get_list();

            /* 2.0 点击新增触发函数 */
            $('.btn_opt').click(function () {
                add_class();
            });

            /* 3.0 点击编辑触发函数 */
            changeclass();

            /* 4.0 点击删除触发函数 */

        });

        /* 1.0 获取文章分类数据 */
        function get_list() {
            /* 1.1 调用myajax 函数获取 */
            myAjax('get', '/admin/category/list', null, function (gl) {
                /* 返回数据类型
                {
               "code": 200,
               "msg": "获取成功",
               "data": [{
                   "id": 1,
                   "name": "爱生活",
                   "slug": "热爱生活"
               }, {
                   "id": 2,
                   "name": "爱旅行",
                   "slug": "热爱旅行"
               }]
               }*/
                /* 1.2 填充数据 */
                let resHtml = template('art_list', gl);
                /* 1.3 添加到列表中 */
                $('#tbody').html(resHtml);
            })
        }


        /* 2.0 新增分类函数 */
        function add_class() {
            let name = $('#name').val();
            let slug = $('#slug').val();

            let arr = { name: name, slug: slug };

            /* 2.1 发送新增请求 */
            myAjax('post', '/admin/category/add', arr, function (gl) {
                /* 2.2 进行判断是否成功响应 */
                if (gl.code !== 200) {
                    alert(gl.msg);
                    return;
                }
                /* 2.3 请求成功加载页面 */
                get_list();

                /* 2.4 关闭模态框 */
                $('#myModal').modal('hide');

                /* 2.5 清空文本框的数据 */
                $('#name').val('');
                $('#slug').val('');
            })
        }


        //定义 编辑的id 变量 为 Id
        let Id = 0;

        /* 3.0 寻找相对id 的数据填充相对应的元素中的函数 */
        function edit_open(c_id) {
            /* 3.1 给Id 赋值 用于下面编辑函数请求传参使用 */
            Id = c_id;

            /* 3.2 开启模态框 */
            $('#change_myModal').modal('show');

            /* 3.3 获取id 中的原有数据 */
            myAjax('get', '/admin/category/search', { id: c_id }, function (gl) {
                /* 响应回来的数据有
                {
                    "code": 200,
                    "msg": "获取成功",
                    "data": [{
                        "id": 2,
                        "name": "爱旅行",
                        "slug": "热爱旅行"
                    }]
                } */

                /* 3.3.1 判断请求是否成功 */
                if (gl.code !== 200) {
                    layer.msg(gl.msg);
                    return;
                }
                /* 3.3.2 判断id是否有获取到数据 */
                if (gl.length <= 0 || !gl.data) {
                    layer.msg('您输入的id参数异常');
                    return;
                }
                // console.log(2);
                // 3.3.3 将老数据显示在网页上
                let data = gl.data[0];
                // console.log(data);
                $('#change_name').val(data.name);
                $('#change_slug').val(data.slug);
            });


        }

        /* 4.0 编辑分类函数 */
        function changeclass() {

            /* 给编辑模态窗中的保存点击事件 */
            $('#edit').click(function () {

                /* 4.1 获取修改后的数据上传函数 */
                let name = $('#change_name').val();
                let slug = $('#change_slug').val();
                let arr = { id: Id, name: name, slug: slug };

                /* 4.1.1 发送新增请求 */
                myAjax('post', '/admin/category/edit', arr, function (gl) {
                    /* 4.1.2 进行判断是否成功响应 */
                    if (gl.code !== 200) {
                        layer.msg(gl.msg);
                        return;
                    } else {
                        layer.msg('修改成功')
                    }

                    /* 4.1.3 请求成功加载页面 */
                    get_list();

                    /* 4.1.4 关闭模态框 */
                    $('#change_myModal').modal('hide');
                })
            })
        }


        /* 4.0 删除分类函数 */
        function del_class(d_id) {

            /* 4.1 弹出删除确认框给用户确认 */
            layer.confirm('确定删除数据?', { icon: 3, title: '删除确认' }, function (i) {

                /* 4.1.1 发送删除请求 */
                myAjax('post', '/admin/category/delete', { id: d_id }, function (gl) {
                    //4.1.2 进行判断
                    if (gl.code !== 200) {
                        layer.msg(gl.msg);
                        return;
                    }
                    /* 4.2 刷新列表 */
                    get_list();
                })
                layer.close(i);
            })
        }
    </script>
</body>

</html>